from PyQt5.Qt import *

"""
QThread
线程类

常用方法
start(): 启动线程
wait(): 阻止线程
sleep(): 强制当前线程休眠
started(): 启动线程
finished(): 结束线程

"""
class MyQWidget(QWidget):
    def __init__(self):
        super().__init__()
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle("QThread")
        self.thread = Worker()
        self.thread.sinOut.connect(self.run)
        self.thread.start()

    def run(self, file_str):
        print(file_str)

class Worker(QThread):

    sinOut = pyqtSignal(str)

    def __init__(self):
        super().__init__()
        self.working = True
        self.num = 0

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        while self.working == True:
            file_str = 'num is {0}'.format(self.num)
            self.num += 1;
            self.sinOut.emit(file_str)
            self.sleep(2)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MyQWidget()
    window.show()
    sys.exit(app.exec_())